package mypackage;

import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;

public class ConnectionString {
	
	/**
     * Determines what connection type to use and returns the necessary string to use it.
     * @return A string with the connection info
     */
    public static String getConnectionString()
    {
    	
        // This code is based on the connection code developed by Mike Nelson of AccelGolf.
        // http://blog.accelgolf.com/2009/05/22/blackberry-cross-carrier-and-cross-network-http-connection        
        String connectionString = null;                
                        
        // Simulator behavior is controlled by the USE_MDS_IN_SIMULATOR variable.
        if(DeviceInfo.isSimulator())
        {
                //logMessage("Device is a simulator and USE_MDS_IN_SIMULATOR is false");
        		connectionString = ";deviceside=false";
        }                                        
                
        // Wifi is the preferred transmission method
        else if((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) &&(WLANInfo.getAPInfo()!=null) )
        {
        	
            //logMessage("Device is connected via Wifi.");
        	connectionString = ";interface=wifi";
        }
                        
        /* Is the carrier network the only way to connect?
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
            //logMessage("Carrier coverage.");
                        
            String carrierUid = getCarrierBIBSUid();
            if(carrierUid == null) 
            {
                // Has carrier coverage, but not BIBS.  So use the carrier's TCP network
                //logMessage("No Uid");
                connectionString = ";deviceside=true";
            }
            else 
            {
                // otherwise, use the Uid to construct a valid carrier BIBS request
                //logMessage("uid is: " + carrierUid);
                connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
            }
        } */               
        
        // Check for an MDS connection instead (BlackBerry Enterprise Server)
        else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
        {
        	//CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS);
           // logMessage("MDS coverage found");
        	connectionString = ";deviceside=false;mds-public";
        }
        
        // If there is no connection available abort to avoid bugging the user unnecssarily.
        else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
        {
          //  logMessage("There is no available connection.");
        	connectionString = "error";
        }
        
        // In theory, all bases are covered so this shouldn't be reachable.
        else
        {
        	 //logMessage("no other options found, assuming device.");
        	connectionString = ";deviceside=true";
        	String name = RadioInfo.getNetworkName(0);
        	if (name.equals("DIGITEL GSM"))
        	{
        		connectionString += ";apn=gprsweb.digitel.ve;tunnelauthusername=guest;tunnelauthpassword=guest";
        	}
        	else if (name.equals("movistar"))
        	{
        		connectionString += ";apn=internet.movistar.ve";
        	}

        	
        }        
        
        return connectionString;
    }
}
